/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.manolovn.sample.slice;

import com.github.clans.fab.FloatingActionButton;
import com.manolovn.sample.ResourceTable;
import com.manolovn.sample.color.BrewerColorGenerator;
import com.manolovn.sample.point.PointGeneratorFactory;
import com.manolovn.trianglify.TrianglifyView;
import com.manolovn.colorbrewer.ColorBrewer;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

import ohos.agp.components.Slider;
import ohos.agp.components.Component;
import ohos.agp.components.Picker;
import ohos.agp.components.ComponentContainer;

/**
 * MainAbilitySlice entity
 */
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private TrianglifyView trianglifyView;
    private Slider cellSizeControl;
    private Slider varianceControl;
    private Picker colorControl;
    private Picker pointsControl;
    private ComponentContainer controlsContainer;
    private FloatingActionButton floatingActionButton;
    private ColorBrewer[] colors;
    private String[] pointItems;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initUIComponents();
        initCellSizeControl();
        initVarianceControl();
        initColorControl();
        initColorControlListener();
        initPointsControl();
        initPointsControlListener();
    }

    /**
     * to initialize all UI Components
     */
    public void initUIComponents(){
        trianglifyView = (TrianglifyView) findComponentById(ResourceTable.Id_trianglify);
        cellSizeControl = (Slider) findComponentById(ResourceTable.Id_cellSizeControl);
        varianceControl = (Slider) findComponentById(ResourceTable.Id_varianceControl);
        colorControl = (Picker) findComponentById(ResourceTable.Id_colorControl);
        pointsControl = (Picker) findComponentById(ResourceTable.Id_pointsControl);
        controlsContainer = (ComponentContainer) findComponentById(ResourceTable.Id_controlsContainer);
        floatingActionButton = (FloatingActionButton) findComponentById(ResourceTable.Id_toggleFullscreen);
        floatingActionButton.setClickedListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void initCellSizeControl() {
        cellSizeControl.setValueChangedListener(new Slider.ValueChangedListener() {
            int progress;

            @Override
            public void onProgressUpdated(Slider slider, int progress, boolean fromUser) {
                this.progress = progress;
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
                if (progress > 5) {
                    trianglifyView.getDrawable().setCellSize(progress * 10);
                    trianglifyView.invalidate();
                }
            }
        });
    }

    private void initVarianceControl() {
        varianceControl.setValueChangedListener(new Slider.ValueChangedListener() {
            int progress;

            @Override
            public void onProgressUpdated(Slider slider, int progress, boolean fromUser) {
                this.progress = progress;
            }

            @Override
            public void onTouchStart(Slider slider) {
            }

            @Override
            public void onTouchEnd(Slider slider) {
                trianglifyView.getDrawable().setVariance(progress + 2);
                trianglifyView.invalidate();
            }
        });
    }

    private void initColorControl() {
        final String[] colorItems = new String[ColorBrewer.values().length];
        colors = ColorBrewer.values();

        for(int i = 0; i < colors.length; i++){
            ColorBrewer color = colors[i];
            colorItems[i] = color.name();
        }
        colorControl.setMaxValue(colorItems.length);
        colorControl.setDisplayedData(colorItems);

        trianglifyView.getDrawable().setColorGenerator
                (new BrewerColorGenerator(colors[colorControl.getValue() - 1]));

        colorControl.setValue(colorControl.getValue() - 1);

        trianglifyView.invalidate();
    }

    private void initColorControlListener(){
        colorControl.setValueChangedListener(new Picker.ValueChangedListener() {
            @Override
            public void onValueChanged(Picker picker, int position, int id) {
                if(colorControl.getValue() == colors.length) {
                    return;
                }

                trianglifyView.getDrawable().setColorGenerator
                        (new BrewerColorGenerator(colors[colorControl.getValue()]));

                trianglifyView.invalidate();
            }
        });
    }

    private void initPointsControl() {
        pointItems = new String[PointGeneratorFactory.Type.values().length];
        final PointGeneratorFactory.Type[] points = PointGeneratorFactory.Type.values();

        for(int i = 0; i < points.length; i++){
            pointItems[i] = points[i].toString();
        }
        pointsControl.setMaxValue(pointItems.length);
        pointsControl.setDisplayedData(pointItems);

        trianglifyView.getDrawable().setPointGenerator(
                PointGeneratorFactory.from(pointItems[pointsControl.getValue() - 1]));

        pointsControl.setValue(pointsControl.getValue() - 1);

        trianglifyView.invalidate();
    }

    private void initPointsControlListener(){
        pointsControl.setValueChangedListener(new Picker.ValueChangedListener() {
            @Override
            public void onValueChanged(Picker picker, int position, int id) {
                if(pointsControl.getValue() == pointItems.length) {
                    return;
                }

                trianglifyView.getDrawable().setPointGenerator(
                        PointGeneratorFactory.from(pointItems[pointsControl.getValue()]));

                trianglifyView.invalidate();
            }
        });
    }

    @Override
    public void onClick(Component component) {
        switch(component.getId()) {
            case ResourceTable.Id_toggleFullscreen:
                if (controlsContainer.getVisibility() == Component.INVISIBLE) {
                    controlsContainer.setVisibility(Component.VISIBLE);
                } else if (controlsContainer.getVisibility() == Component.VISIBLE) {
                    controlsContainer.setVisibility(Component.INVISIBLE);
                }
                break;
        }
    }
}
